<?php
    namespace app\admin\model;
    use app\common\model\Model;
    use think\Db;
    
//  model 类
    class GoodsModel extends Model
    {
        const TABLE_NAME = 'goods';
        const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_4;

        protected $name = self::TABLE_NAME;

        const FIELDS= 'goods_id,goods_name,goods_sn,goods_type,goods_cate,price,formal_price,stock,goods_sell_num,goods_img,describe,is_show,goods_points,goods_plat';
        const GOODS_IMAGE_PATH='goods';

        // 修改商品上架状态
        public static function updateGoodsStatus($status=null,$goods_id=null){


            return self::commonQuery(self::TABLE_NAME)
                ->where(['goods_id'=>$goods_id])
                ->setField('is_show',$status);
            

        }
       
        // 根据ID获取商品信息
        public static function getGoodsInfoById($id,$field=''){
            if(empty($id)){
                return [];
            }

            if($field == ''){
                $field=self::FIELDS;
            }
            return self::commonQuery(self::TABLE_NAME,self::CACHE_EXPIRE_LEVEL_2)
                ->field($field)->where('goods_id',$id)->find();

        }

        //获取 商品ID
        public static function getGoodsIdByName($name)
        {
            return self::commonQuery(self::TABLE_NAME)
                   ->where(['goods_name'=>'like','%'.$name.'%'])
                   ->column('goods_id');
        }
        //修改 商品积分
        public static function updateGoodsPoints($goods_id,$points)
        {
            return self::commonQuery(self::TABLE_NAME)
                ->where(['goods_id'=>$goods_id])
                ->setField('goods_points',$points);
        }
        // 删除商品
        public static function delGoodsInfo($id){
            // if (!$id) {
            //     return false;
            // }

           return self::commonQuery(self::TABLE_NAME)->where('goods_id',$id)->update(['is_del'=>1]);

        }

       //获取 商品列表
        public static function getGoodsListQuery($good_cate=0,$goods_plat=0){

            $query = self::commonQuery(self::TABLE_NAME)
                    ->field(self::FIELDS)
                   ->where(['is_del'=>0]);

            if($good_cate > 0)
            {
                $query->where(['goods_cate'=>$good_cate]);
            }
            if($goods_plat >0){
               // $query->where(['goods_plat'=>['in',[0,$goods_plat]]]);
               // $query->where(['exp'=>"goods_plat & $goods_plat"]);
                //$query->whereExp('goods_plat'," & $goods_plat");
                $query->where(['goods_plat'=>$goods_plat]);
            }
            return $query;

        }
        public static function handleGoodsInfo($info,$user_id)
        {
            $info['goods_img']=get_image_url(self::GOODS_IMAGE_PATH,$info['goods_img']);

            $info['goods_cate']=self::getGoodsCateName($info['goods_cate']);
            return $info;
        }
        public static function getGoodsCount($good_cate =0,$goods_plat=0)
        {
            return self::getGoodsListQuery($good_cate,$goods_plat)->count();
        }
        //取消订单时， 添加 库存 和 减少 卖出数
        public static function incrGoodsNum($goods_id,$num)
        {
            return self::commonQuery(self::TABLE_NAME)
                ->where(['goods_id'=>$goods_id])
                ->exp("goods_sell_num","goods_sell_num - $num")
                ->setInc('stock',$num);
        }

        public static function getGoodsCateList($cate_plat=0)
        {
            return self::getGoodsCateQuery($cate_plat)->select();
        }
        public static function getGoodsCateQuery($cate_plat=0)
        {
            $query= self::commonQuery('goods_cate_list');

            if($cate_plat > 0){
              //  $query->where(['cate_plat'=>['in',0,$cate_plat]]);
              //  $query->whereExp('cate_plat'," & $cate_plat");
                $query->where(['cate_plat'=>$cate_plat]);
            }
            return $query;
        }
        public static function getGoodsCateName($good_cate_id)
        {
            $info = self::getGoodsCateInfo($good_cate_id);
            return empty($info)?'':$info['cate_name'];
        }
       
        public static function getGoodsCateInfo($good_cate_id,$cache=true){
            if(!$cache)
            {
                $query = self::commonQuery('goods_cate_list',self::CACHE_EXPIRE_LEVEL_4);
            }else{
                $query = self::commonQuery('goods_cate_list');
            }
           
            return  $query->where(['cate_id'=>$good_cate_id])->find();
        }
        public static function updateGoodsCateInfo($goods_cate_id,$goods_cate_name,$goods_cate_order,$cate_plat=0)
        {
            $query = self::commonQuery('goods_cate_list');

            $data = ['cate_order'=>$goods_cate_order];

            if(!empty($goods_cate_name))
            {
                $data['cate_name']=$goods_cate_name;
            }
            $data['cate_plat']=$cate_plat;
            if($goods_cate_id >0)
            {
                return $query->where(['cate_id'=>$goods_cate_id])->update($data);
            }else{
                return $query->insert($data);
            }
        }
        public static function deleteGoodsCate($goods_cate_id)
        {
            return self::commonQuery('goods_cate_list')->where(['cate_id'=>$goods_cate_id])->delete();
        }

        public static  function getPlatList(){
            return [
                '微信公众号',
                '网站',
//                '小程序',
//                'test1',
//                'test2'
            ];
        }

       private static function calcPlatNum($plats){
            


            $all_plat_list = self::getPlatList();


            $count =count($all_plat_list);
            if($count == count($plats)){
                return (1<< $count) -1;
            }
            $res =0 ;
            foreach ($plats as $val){

                if(isset($all_plat_list[$val])){
                    $res += (1 << $val);
                }
            }
            return $res;
        }
        public static function getPlatInfo($plat_num,$get_key=false){
            return $plat_num;
        }
//            $res =[];
//            $all_plat_list = self::getPlatList();
//            $count = count($all_plat_list);
//            if($plat_num == (1<< $count) -1){
//                return  $get_key?array_keys($all_plat_list):$all_plat_list;
//            }
//
//            foreach ($all_plat_list as $key=>$val){
//                $mask = (1<< $key);
//                if(($plat_num & $mask) == $mask){
//                    $res[] = $get_key?$key:$val;
//                }
//            }
//            return $res;
//        }
    }

